package com.hashcoding.eyes.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.entity.Photo;
import com.hashcoding.eyes.ui.widget.ObservableScrollView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhotoActivity extends BaseActivity implements ObservableScrollView
        .OnScrollChangedListener {

    private final static String KEY_PHOTO = "photos";

    @Bind(R.id.image_view)
    ImageView mImageView;
    @Bind(R.id.photo_name)
    TextView mPhotoName;
    @Bind(R.id.photo_description)
    TextView mPhotoDescription;
    @Bind(R.id.photo_address)
    TextView mPhotoAddress;
    @Bind(R.id.scrollView)
    ObservableScrollView mScrollView;
    @Bind(R.id.toolbar_bg_green)
    ImageView mToolbarBgGreen;
    @Bind(R.id.title_left_btn)
    ImageView mTitleLeftBtn;
    @Bind(R.id.star)
    TextView mStar;

    private Photo mPhoto;

    public static void launch(Context context, Photo photo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY_PHOTO, photo);

        Intent intent = new Intent(context, PhotoActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);

        mPhoto = getIntent().getParcelableExtra(KEY_PHOTO);

        initView();
    }

    private void initView() {
        mToolbarBgGreen.setAlpha(0f);
        mScrollView.setOnScrollChangedListener(this);

        Glide.with(this).load(mPhoto.url)
                .placeholder(R.drawable.service_bg)
                .error(R.drawable.icon_empty)
                .bitmapTransform(new CenterCrop(this))
                .into(mImageView);

        mPhotoName.setText(mPhoto.title);
        mPhotoDescription.setText(mPhoto.description);
        mPhotoAddress.setText("地点：成都");
        mStar.setText(mPhoto.isStart ? "取消赞" : "点赞");
    }

    @OnClick(R.id.title_left_btn)
    protected void onTitleLeftClick() {
        onBackPressed();
    }

    @OnClick(R.id.image_view)
    protected void onImageViewClick() {
        PhotosViewActivity.luanch(this, mPhoto.url);
    }

    @OnClick(R.id.star)
    protected void onStarClick() {
        mPhoto.isStart = !mPhoto.isStart;
        shortToast(mPhoto.isStart ? "点赞成功" : "取消成功");
        mStar.setText(mPhoto.isStart ? "取消赞" : "点赞");
    }

    @OnClick(R.id.share)
    protected void onShareClick() {
        Intent intent=new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
        intent.putExtra(Intent.EXTRA_TEXT, mPhoto.title + " (分享自Eyes)");

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getTitle()));
    }

    @Override
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        float ratio = mScrollView.getScrollY() * 1.0f /
                (mImageView.getHeight() - mToolbarBgGreen.getHeight());
        if (ratio < 1) {
            mToolbarBgGreen.setAlpha(ratio);
        } else {
            mToolbarBgGreen.setAlpha(1f);
        }
    }
}
